import 'package:flutter/material.dart';

class ExpansionPanelDemo extends StatefulWidget {
  ExpansionPanelDemo({Key key}) : super(key: key);

  @override
  _ExpansionPanelDemoState createState() => _ExpansionPanelDemoState();
}

class _ExpansionPanelDemoState extends State<ExpansionPanelDemo> {

  List<ExpansionPanelItem> _panelItems;

  @override
  void initState() {
    super.initState();
    _panelItems = <ExpansionPanelItem> [
      ExpansionPanelItem(
        headerText: "panel A",
        body: Container(
          padding: EdgeInsets.all(16.0),
          width: double.infinity,
          child: Text("Content for Panel A."),
        ),
        isExpanded: false,
      ),
      ExpansionPanelItem(
        headerText: "panel B",
        body: Container(
          padding: EdgeInsets.all(16.0),
          width: double.infinity,
          child: Text("Content for Panel B."),
        ),
        isExpanded: false,
      ),
      ExpansionPanelItem(
        headerText: "panel C",
        body: Container(
          padding: EdgeInsets.all(16.0),
          width: double.infinity,
          child: Text("Content for Panel C."),
        ),
        isExpanded: false,
      ),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("ExpansionPanelDemo"),
        elevation: 0.0,
      ),
      body: Container(
        padding: EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ExpansionPanelList(
              expansionCallback: (int panelIndex, bool isExpanded) {
                setState(() {
                  _panelItems[panelIndex].isExpanded = !_panelItems[panelIndex].isExpanded;
                });
              },
              children: _panelItems.map(
                (ExpansionPanelItem panelItem) {
                  return ExpansionPanel(
                    headerBuilder: (BuildContext context, bool isExpand) {
                      return Container(
                        padding: EdgeInsets.all(16.0),
                        child: Text(
                          panelItem.headerText,
                          style: TextStyle(
                            fontSize: 24.0,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      );
                    }, 
                    body: panelItem.body,
                    isExpanded: panelItem.isExpanded,
                  );
                }
              ).toList(),
            )
          ],
        ),
      ),
    );
  }
}

class ExpansionPanelItem {
  final String headerText;
  final Widget body;
  bool isExpanded;

  ExpansionPanelItem({
    this.headerText,
    this.body,
    this.isExpanded,
  });

}